<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Cache;

class Index extends Controller
{
	protected function _initialize() {
		$this->assign('action',$this->request->action());
	}
	//首页
    public function index()
    {
    	$newad = db('gw_news')->where('pid',5)->limit(3)->order('add_time desc')->select();
    	$this->assign('newad',$newad);
    	$newlist = db('gw_news')->where('pid',4)->limit(3)->order('add_time desc')->select();
    	$this->assign('newlist',$newlist);
    	
    	$zplist = db('gw_news')->where('pid',6)->limit(8)->order('add_time desc')->select();
    	$this->assign('zplist',$zplist);
    	$szlist = db('gw_alist')->where('pid',2)->limit(4)->order('sort desc')->select();
    	$this->assign('szlist',$szlist);
    	$ad = db('gw_ads')->where('action','index')->order('sort asc')->select();
    	$this->assign('ad',$ad);
    	return $this->fetch();
    }
    //学校简介
    public function about($id='1',$pid='')
    {
    	$res = db('gw_about')->where('pid',1)->select();
    	$this->assign('list',$res);
    	
    	$about = db('gw_about')->where('id',$id)->find();
    	$this->assign('about',$about);
    	
    	$ad = db('gw_ads')->where('action','about')->order('sort asc')->find();
    	$this->assign('ad',$ad);
    	
    	$this->assign('pid',$pid);
    	
    	return $this->fetch();
    }
    //学校简介
    public function alist($pid=1)
    {
    	$about['id']='';
    	$res = Db::name('gw_alist')->where('pid',$pid)->paginate(5, false, get_query());
		$this->assign('data',$res);
		$this->assign('pid',$pid);
		
		$ad = db('gw_ads')->where('action','about')->order('sort asc')->find();
    	$this->assign('ad',$ad);
    	
    	$this->assign('about',$about);
    	return $this->fetch();
    }
    public function alistshow($id='')
    {
    	$about['id']='';
    	$data = db('gw_alist')->where('id',$id)->find();
    	$this->assign('data',$data);
    	$ad = db('gw_ads')->where('action','about')->order('sort asc')->find();
    	$this->assign('ad',$ad);
    	$this->assign('about',$about);
    	return $this->fetch();
    }
    //热门课程
    public function hotcourse($id='6')
    {
    	$res = db('gw_about')->where('pid',2)->select();
    	$this->assign('list',$res);
    	
    	$about = db('gw_about')->where('id',$id)->find();
    	$this->assign('about',$about);
    	$ad = db('gw_ads')->where('action','hotcourse')->order('sort asc')->find();
    	$this->assign('ad',$ad);
    	return $this->fetch();
    }
    
    //学院动态
    public function newslist($pid=4,$fid=1)
    {
    	$res = Db::name('gw_news')->where('pid',$pid)->paginate(5, false, get_query());
		$this->assign('data',$res);
		$type = Db::name('gw_newscate')->where('pid',$fid)->select();
		$this->assign('type',$type);
		$this->assign('pid',$pid);
		
		$ad = db('gw_ads')->where('action','newslist')->order('sort asc')->find();
    	$this->assign('ad',$ad);
    	return $this->fetch();
    }
    public function newshow($id,$fid=1)
    {
    	$type = db('gw_newscate')->where('pid',$fid)->select();
    	$this->assign('type',$type);
    	
    	$data = db('gw_news')->where('id',$id)->find();
    	db('gw_news')->where('id',$id)->setInc('click',2);
    	$this->assign('data',$data);
    	$ad = db('gw_ads')->where('action','newslist')->order('sort asc')->find();
    	$this->assign('ad',$ad);
    	return $this->fetch();
    }
    //学员就业
    public function joblist($pid=6,$fid=2)
    {
    	$res = Db::name('gw_news')->where('pid',$pid)->paginate(5, false, get_query());
		$this->assign('data',$res);
		$type = Db::name('gw_newscate')->where('pid',$fid)->select();
		$this->assign('type',$type);
		$this->assign('pid',$pid);
		$ad = db('gw_ads')->where('action','joblist')->order('sort asc')->find();
    	$this->assign('ad',$ad);
    	return $this->fetch();
    }
    public function jobshow($id,$fid=2)
    {
    	$type = db('gw_newscate')->where('pid',$fid)->select();
    	$this->assign('type',$type);
    	
    	$data = db('gw_news')->where('id',$id)->find();
    	db('gw_news')->where('id',$id)->setInc('click');
    	$this->assign('data',$data);
    	$ad = db('gw_ads')->where('action','joblist')->order('sort asc')->find();
    	$this->assign('ad',$ad);
    	return $this->fetch();
    }
    
    //招生问答
    public function answerslist($pid=8,$fid=3)
    {
    	$res = Db::name('gw_news')->where('pid',$pid)->paginate(5, false, get_query());
		$this->assign('data',$res);
		$type = Db::name('gw_newscate')->where('pid',$fid)->select();
		$this->assign('type',$type);
		$this->assign('pid',$pid);
		$ad = db('gw_ads')->where('action','answerslist')->order('sort asc')->find();
    	$this->assign('ad',$ad);
    	return $this->fetch();
    }
    public function answershow($id,$fid=3)
    {
    	$type = db('gw_newscate')->where('pid',$fid)->select();
    	$this->assign('type',$type);
    	
    	$data = db('gw_news')->where('id',$id)->find();
    	db('gw_news')->where('id',$id)->setInc('click');
    	$this->assign('data',$data);
    	$ad = db('gw_ads')->where('action','answerslist')->order('sort asc')->find();
    	$this->assign('ad',$ad);
    	return $this->fetch();
    }
    
    //通知公告
    public function notice()
    {
    	$res = Db::name('gw_message')->where('pid',2)->paginate(5, false, get_query());
		$this->assign('data',$res);
		$ad = db('gw_ads')->where('action','notice')->order('sort asc')->find();
    	$this->assign('ad',$ad);
    	return $this->fetch();
    }
    public function noticeshow($id)
    {
    	$data = db('gw_message')->where('id',$id)->find();
    	$this->assign('data',$data);
    	$ad = db('gw_ads')->where('action','notice')->order('sort asc')->find();
    	$this->assign('ad',$ad);
    	return $this->fetch();
    }
    //在线留言
    public function message()
    {
    	
    	if($this->request->isPost()){
			$data = $this->request->post();
			$data['add_time'] = now_time();
			$res = db('gw_message')->insert($data);
			if($res){
				return $this->success('留言成功');
			}else{
				return $this->error('留言失败');
			}
		}else{
			$ad = db('gw_ads')->where('action','notice')->order('sort asc')->find();
	    	$this->assign('ad',$ad);
	    	return $this->fetch();
		}
    }
    
    //我要报名
    public function baoming()
    {
    	
    	if($this->request->isPost()){
			$data = $this->request->post();
			$data['add_time'] = now_time();
			$data['pid'] = 3;
			$res = db('gw_message')->insert($data);
			if($res){
				return $this->success('报名成功');
			}else{
				return $this->error('报名失败');
			}
		}else{
			$ad = db('gw_ads')->where('action','notice')->order('sort asc')->find();
	    	$this->assign('ad',$ad);
	    	return $this->fetch();
		}
    }
    
}
